/**
 * @(#)MonitorMobileServiceImpl.java 2017年2月15日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.monitor.service;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.common.monitor.MonitorContentTemplate;
import org.jsbd.boss.dao.IDmChannelRetentDAO;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.monitor.MonitorMobileInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 手机号获取率统计类
 * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
 * @version 1.0.0
 */
@Service("monitorMobileService")
public class MonitorMobileServiceImpl  extends MonitorBaseService{
	public static final Logger LOGGER = LoggerFactory.getLogger(MonitorMobileServiceImpl.class);

	@Autowired
	private IDmChannelRetentDAO dmChannelRetentDAO;
	
	public Map<String,String> getMonitorContent(List<ChannelApkConfig> apkList,Map<String,ChannelApkConfig> apkConfigMap
			 ,Map<String,Object> conditionMap){
		 
        Map<String, String> resultMap=null;
		try{
		    List<MonitorMobileInfo> resultList=getMonitorContentList(apkConfigMap,conditionMap);
			return getMonitorResultMap(resultList,MonitorContentTemplate.MOBILE_TABLE_HEADER,"MOBILE_RATE");	
		}catch(Exception e){
		    LOGGER.error("",e);
			resultMap=productExceptionMap(e,"手机获取数监控");
		}
		 
		 return resultMap;
	}
	
    public List<MonitorMobileInfo> getMonitorContentList(Map<String,ChannelApkConfig> apkConfigMap,Map<String,Object> conditionMap){
    	
    	//从数据库获取数据
		List<Map<String, Object>> dataList = dmChannelRetentDAO.getMarkPhoneRateFromRetent(conditionMap);
	    if(dataList==null||dataList.isEmpty()){
	    	LOGGER.info("getMarkPhoneRateFromRetent is null and param is {}",conditionMap);
	    	return null;
	    }
	    
	    List<MonitorMobileInfo> taskList=new ArrayList<MonitorMobileInfo>();
	    List<MonitorMobileInfo> fillAllList=new ArrayList<MonitorMobileInfo>();
	    
	    Long markTotalNum=0L;
	    Long markTotalUnEmptyNum=0L;
	    
	    for(Map<String, Object> dataMap: dataList){
	    	String mark=(String)dataMap.get("mark");
	    	long mobileTotal=(long)dataMap.get("total");
	    	long mobileUnEmptyNum=((BigDecimal)dataMap.get("mobileUnEmptyNum")).longValueExact();
	    	double mobileUnEmptyRate=((BigDecimal)dataMap.get("mobileUnEmptyRate")).doubleValue();
	    	
	    	MonitorMobileInfo taskInfo=new MonitorMobileInfo();
	    	taskInfo.setItemKey("get_mobile_rate");
	    	taskInfo.setMark(mark);
	    	taskInfo.setMobileTotal(mobileTotal);
	    	taskInfo.setMobileUnEmptyNum(mobileUnEmptyNum);
	    	taskInfo.setMobileUnEmptyRate(mobileUnEmptyRate);
	    	
	    	ChannelApkConfig config=apkConfigMap.get(mark);
	    	if(config==null){
	    		taskInfo.setApkName("");
	    		taskInfo.setChannelName("");
	    	}else{
	    		taskInfo.setApkName(config.getApkName());
	    		taskInfo.setChannelName(config.getChannelName());
	    	}
	    	
	    	//当手机获取率为0时,则任务该任务是获取不到手机的，不计算在内
	    	if(mobileUnEmptyRate>0){
	    		markTotalNum+=mobileTotal;
		    	markTotalUnEmptyNum+=mobileUnEmptyNum;
		    	taskList.add(taskInfo);
	    	}else{
	    		fillAllList.add(taskInfo);
	    	}
	    }
	    
	    Collections.sort(taskList, new MobileRateComparator());
	    taskList.addAll(fillAllList);
	    
	    //统计平均值
	    MonitorMobileInfo taskInfo=new MonitorMobileInfo();
    	taskInfo.setMark("all");
    	taskInfo.setApkName("均值");
    	taskInfo.setMobileTotal(markTotalNum);
    	taskInfo.setMobileUnEmptyNum(markTotalUnEmptyNum);
    	if(taskInfo.getMobileTotal()==null){
    		taskInfo.setMobileUnEmptyRate(0.00);
    	}else{
    		taskInfo.setMobileUnEmptyRate(taskInfo.getMobileUnEmptyNum()*1.0/taskInfo.getMobileTotal());
    	}
    	
    	taskList.add(0, taskInfo);
    	
    	validateOk(taskList,taskInfo);
    	return taskList;
    }
	
    /**
     * 
     * 小于均值-10%以上就标红
     * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
     * @param taskList
     * @param compareTaskInfo
     */
    public void validateOk(List<MonitorMobileInfo> taskList,MonitorMobileInfo compareTaskInfo){
    	double emptyRate=compareTaskInfo.getMobileUnEmptyRate()-0.1;
    	for(MonitorMobileInfo mobileInfo:taskList){
    		double recordRate=mobileInfo.getMobileUnEmptyRate();
    		if(recordRate<=emptyRate&&recordRate!=0){
    			mobileInfo.setColour("red");
    		}
    	}
    	
    }
    
    public String productEmailContent(List<MonitorMobileInfo> contentInfoList){
    	return MonitorContentTemplate.getHtmlTableInfo(contentInfoList, MonitorContentTemplate.MOBILE_TABLE_HEADER);
    }
    
    static class MobileRateComparator implements Comparator<MonitorMobileInfo> {
	    @Override
		public int compare(MonitorMobileInfo arg0, MonitorMobileInfo arg1) {
	    	Double data1=Double.valueOf(arg1.getMobileUnEmptyRate());
	    	Double data0=Double.valueOf(arg0.getMobileUnEmptyRate());
	    	
	    	if(data1-data0==0){
	    		return 0;
	    	}
	    	if(data1-data0>0){
	    		return -1;
	    	}else{
	    		return 1;
	    	}
		}
	}
	
}
